<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="main.css">
<%@ page import="java.io.*"%>
<%@ page import="java.util.*" %>
<%@ page import="model.Target"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="ejb.TargetFacadeEJBRemote" %> 
<%@ page import="model.Target" %>
<%@ page import="model.Apply" %>
<%@ page import="model.Tag" %>
<%@ page import="model.Mycomment" %>


<title>Target Page</title>

<script type="text/javascript">

function submitTarget(){
 	var keyword=document.getElementById("hidden_t").value;
   	window.location.href="TargetServlet?keyword="+keyword;
   	
}

function pass(){
	var patrn1=/^(\w){1,10}$/;
    if((document.getElementById("tagform").value)==""){
        alert("tag is empty!");
        return false;
    }
    else if(!patrn1.exec(document.getElementById("tagform").value)){
    	alert("tag format error or too long! it should be only characters and length should not be longer than 10");
    	return false;
    }
    else{
	window.location.href="TargetServlet?tagtid="+document.getElementById("hidden_t").value+"&tag="+document.getElementById("tagform").value;
    return true;
    }
}

function pass2(){
	var patrn2=/^(\w?|\s?|\.|\\|\,|\?|:){1,500}$/;
    if((document.getElementById("merchantinfo").value)==""){
        alert("merchant info is empty!");
        return false;
    }
    else if(!patrn2.exec(document.getElementById("merchantinfo").value)){
    	alert("merchant info format error or too long! it should be only characters and length should not be longer than 10");
    	return false;
    }
    else{
	window.location.href="TargetServlet?tid2="+document.getElementById("hidden_t").value+"&merchantinfo="+document.getElementById("merchantinfo").value;
	return true;
    }
}
function FocusOn()
{
	var id2=window.event.srcElement.id;
	//window.location.href=id2;
	document.getElementById("hidden_user2").value=id2;
	//window.location.href=id2;
	document.getElementById("comment_input").focus();
}
function postto() {
	var aform = document.createElement('form');

    aform.method='post';
    aform.action='AddComment';
    
    var targetid=document.createElement('input');
    targetid.type='hidden';
    targetid.name='targetid';
    targetid.value=document.getElementById("hidden_target").value;
    
    var commentstr=document.createElement('input');
    commentstr.type='hidden';
    commentstr.name='commentstr';
    commentstr.value=document.getElementById("comment_input").value;
    
    var user1=document.createElement('input');
    user1.type='hidden';
    user1.name='user1';
    user1.value=document.getElementById("hidden_user1").value;
    
    var user2=document.createElement('input');
    user2.type='hidden';
    user2.name='user2';
    user2.value=document.getElementById("hidden_user2").value;
    
    aform.appendChild(targetid);
    aform.appendChild(commentstr);
    aform.appendChild(user1);
    aform.appendChild(user2);
    
    document.body.appendChild(aform);
    aform.submit();
	}
function PostComment()
{
	var patrn2=/^(\w?|\s?|\.|\\|\,|\?|:){1,500}$/;
    if((document.getElementById("comment_input").value)==""){
        alert("comment is empty!");
        return false;
    }
    else if(!patrn2.exec(document.getElementById("comment_input").value)){
    	alert("comment format error or too long! its length should not be longer than 500");
    	return false;
    }else{
		postto();
		return true;
    }
}

</script>




</head>
<% String name=(String)session.getAttribute("username");

if(name==null){
    out.print("Please login first! redirecting to Login...");
    response.setHeader("Refresh","1;URL=login.jsp");
}
else{
%>
<body>
<input type="hidden" id="hidden_user1" value="<%=session.getAttribute("username").toString()%>" />
<input type="hidden" id="hidden_user2" name="hidden2" value=""/>
<input type="hidden" id="hidden_reply" value=""/>
<center>
<h1>Target Page: </h1>
<% 

List<Tag> taglist = (List<Tag>)request.getAttribute("taglist");
if(taglist.size()>0){
int count = 0;
out.println("<h2>Current Tags: </h2>");
while(count<taglist.size()){
	out.println( taglist.get(count).getTagname() +"    ");
	count++;
}
}
//get comments from servlet
List<Mycomment> comments=(List<Mycomment>)request.getAttribute("comments");

Target temp = (Target)request.getAttribute("return_t");
out.print("<input type=\"hidden\" id=\"hidden_target\" value=\""+temp.getTargetid()+"\"/>");//create a hidden to record targetid
String targetid = Integer.toString(temp.getTargetid());
String targetname = temp.getTargetname();

out.print("<input type=\"hidden\" id=\"hidden_t\" value=\""+targetid+"\" />");

%>

<form action="TagServlet" name="newtag" method="get">
	<table width="200">
		<tr><td>Add tags to the target:
		<br>
	  	<input type="text" name="tag" id="tagform" style="width: 500px;" onkeypress="if (event.keyCode == 13) {pass(); return false;}"></td></tr>
	  	<tr><td><input type="button" onclick="return pass()" id="tag_btn" value="Submit" ></td></tr>
   	</table>
</form>
<%

if(temp.getMyuser()!=null){
	if((session.getAttribute("username").toString()).equals(temp.getMyuser().getUsername())){
		//out.print("<input type=\"hidden\" id=\"hidden_user1\" value=\""+session.getAttribute("username").toString()+"\" />");
		%>  
		
		<br>
		<form action="TargetServlet" name="minfo" method="get">
			<table width="200">
				<tr><td>Post news to your target here:</td></tr>
	  			<tr><td><textarea name="minfo" cols=69  rows=15 id="merchantinfo"></textarea></td></tr>
	  			<tr><td><input type="button" onclick="pass2()" value="Post" ></td></tr>
   			</table>
		</form>
		<%
	}
}

if(temp!=null){
out.println("<h2>Target Name: " + temp.getTargetname() + "</h2>");
out.println("<h2>Target Location: " + temp.getLocation() + "</h2>");
out.println("<h2>Target Info: " + temp.getInfo() + "</h2>");
if(temp.getMyuser()!=null){
out.println("<h2>Target Merchant: " + temp.getMyuser().getUsername() + "</h2>");
}else{
out.println("<h2>Target Merchant: NO APPROED MERCHANT YET </h2>");
}
if(temp.getMchtinfo()!=null){
out.println("<h2>Current Merchant Info: " + temp.getMchtinfo() + "</h2>");
}else{
out.println("<h2>Current Merchant Info: NO MERCHANT INFO YET</h2>");
}

if(temp.getActivestatus()==1){
	out.println("<h2>Target Business Status: OPEN" + "</h2>");
}else{
	out.println("<h2>Target Business Status: CLOSED " + "</h2>");
}


}

String liked = (String)request.getAttribute("liked");
String applied = (String)request.getAttribute("Applied");

%> 


<%
if(liked!="true"){ 
 	out.print("<input type=\"image\" src=\"Images/like_button.png\" width=\"60\" height=\"60\" onclick=\"submitTarget()\" value=\"Like\"/>");
}else{
	out.println("<br>");
	out.println("<h2> Thank you. You have already liked this Target. </h2>");
}
%>

<input type="button" onclick="javascript:location.href='ApplyMerchant.jsp?targetID=<%=targetid%>'" value="Apply Merchant" >
<input type="button" onclick="javascript:window.open('SendRecommend.jsp?targetID=<%=targetid%>')" value="Recommend to Friends" >
<input type="button" onclick="javascript:location.href='Home'" value="Back to Home"/>
<%
	if((session.getAttribute("authority").toString()).equals("editor") || session.getAttribute("authority").toString().equals("admin")){
		%>
		<input type="button" onclick="javascript:location.href='TargetServlet?closeTID=<%=targetid%>'" value="Close Target" >
		<input type="button" onclick="javascript:location.href='TargetServlet?openTID=<%=targetid%>'" value="Reopen Target" >
		
		<%
	}
 %>
<table id="comment" style="width:800px;">
<tr><td style="width:200px;">NAME</td><td style="width:500px;">CONTEXT</td><td style="width:100px;">OPERATION</td></tr>

<%
try{
if(comments!=null){
	for(Mycomment mc:comments){
		String context=mc.getContent();
		String username1=mc.getMyuser1().getUsername();
		String date=mc.getPostdate().toString().trim();
		int userid1=mc.getMyuser1().getUserid();
		if(mc.getMyuser2()!=null){
			String username2=mc.getMyuser2().getUsername();
			//int userid2=mc.getMyuser2().getUserid();
		%>
		<tr>
		<td>
		<table style="width:200px;">
		<tr><td><%=username1 %></td></tr> 
		<tr><td> posted at </td></tr>
		<tr><td><%=date %></td></tr>
		</table>
		</td>
		<td style="width:500px;"><p style="width:500px;word-wrap: break-word;">REPLY TO "<%=username2 %>" :<%=context %></p></td>
		<td><input type="button" value="reply" id="<%=username1%>" onclick="FocusOn();"/></td>
		</tr>
		<%


		}
		else{
		%>
			<tr>
		<td>
		<table style="width:200px;">
		<tr><td><%=username1 %></td></tr> 
		<tr><td> posted at </td></tr>
		<tr><td><%=date %></td></tr>
		</table>
		</td>
		<td><p style="width:500px;word-wrap: break-word;"><%=context %></p></td>
		<td><input type="button" value="reply" id="<%=username1%>" onclick="FocusOn();"/></td>
		</tr>
		<%
		}	
	}
}
}catch(Exception e){
	e.printStackTrace();
}
%>
</table>
<table>
<tr><td>post comment:</td></tr>
<tr><td><textarea rows="6" cols="90" id="comment_input">
</textarea></td></tr>
<tr><td align="right"><input type="button" value="POST" onclick="return PostComment();"/></td></tr>
<tr style="height:100px;"><td></td><td></td></tr>
</table>
</center>
</body>
<%} %>



